<?php
namespace Admin\Model;
use Think\Model;
class CateModel extends Model {

		protected $_validate = array(
				array('cate_name','require','栏目名111111111不得为空！',1), //默认情况下用正则进行验证
				array('cate_name','',"文章栏目名称不得重复！",1,'unique',3),
		);
		
		//用查询到的所有数据，调用递归函数，得到排好序后栏目结果的数组；
		public function catetree(){
			$data = $this->order("id desc")->select();
			return $this->tree($data);
	
		}
		
		
		//使用递归方式对商品栏目列表进行排序，返回排序好后的一个数组
		public function tree($data,$pid=0,$level=0){
			static $arr=array();
			foreach($data as $K=>$v){
				if($v['pid']==$pid){
					$v['level']=$level;
					$arr[]=$v;
					$this->tree($data,$v['id'],$level+1);
		
				}
			}
			return $arr;

		}
		
		
		public function catedel($id){
			$datas = $this->select();
			$res =$this->tigei($datas,$id);
			$res['aaa']=$id;
			return $res;
		}
		
		public function tigei($datas,$id){
		static $arr=array();

			foreach($datas as $k=>$vo){
				if($vo['pid']==$id){
					$arr[]=$vo['id'];
					$this->tigei($datas,$vo['id']);
				}
			}
			return $arr;
		}
		
		public function _after_insert($data,$option){
					//处理商品的推荐位
				$recid=I('recid');
				if($recid){
					foreach($recid as $k=>$v){
					D('recvalue')->add(array(
										'valueid'=>$data['id'],
										'recid'=>$v,
										'rectype'=>2
									));
					}
				}
		}
		
		
		public function _after_update($data,$option){
			
			D('recvalue')->where(array('valueid'=>$option['where']['id'],'rectype'=>2))->delete();
					//处理商品的修改推荐位
				$recid=I('recid');
				if($recid){
					foreach($recid as $k=>$v){
					D('recvalue')->add(array(
										'valueid'=>$data['id'],
										'recid'=>$v,
										'rectype'=>2
									));
					}
				}
		}
		
}